#coding=utf-8

import itchat
import random
import json
import threading
from urllib import request

def get_price():
    url = 'http://www.chinawkb.com/Ajax/getJsonTop?market=wkb_cny&t='
    random.seed()
    url = "%s%.16f" % (url, random.random())
    head = {}
    head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0'
    try:
        req = request.Request(url, headers = head)
        rsp = request.urlopen(req)
        html = rsp.read().decode('utf-8')
        info = json.loads(html)
        return info['info']['new_price']
    except:
        print("Error except")
        return 0




def price_notify():
    global timer
    global price

    new_price = get_price()

    if new_price != 0:
        if abs(new_price - price) >= 0.05:
            price = new_price
            itchat.send('%f' % price, toUserName='filehelper')
        else:
            print(new_price)

    timer = threading.Timer(10, price_notify)
    timer.start()

if __name__ == '__main__':
    itchat.auto_login()
    price = 0
    timer = threading.Timer(10, price_notify)
    timer.start()






